package com.tangyee.home_blood_center.fragments;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.tangyee.home_blood_center.bean.BSRecord;
import com.tangyee.home_blood_center.db.BSRecordHolder;
import com.tangyee.home_blood_center.utils.DateUtil;
import com.tangyee.home_blood_center.view.LineGraphicView;
import com.tangyee.R;
import com.tangyee.home_blood_center.bean.BSRecord;
import com.tangyee.home_blood_center.db.BSRecordHolder;
import com.tangyee.home_blood_center.utils.DateUtil;
import com.tangyee.home_blood_center.view.LineGraphicView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 和BloodPressureChartFragment差不多，只不过这里又8张曲线图
 */
public class BloodSugarChartFragment7Days extends BaseFragment {

    private ArrayList<Double> yList1;
    private ArrayList<Double> yList2;
    private ArrayList<Double> yList3;
    private ArrayList<Double> yList4;
    private ArrayList<Double> yList5;
    private ArrayList<Double> yList6;
    private ArrayList<Double> yList7;
    private ArrayList<Double> yList8;
    private ArrayList<String> xRawDatas;
    private LineGraphicView mLineGraphicView1;
    private LineGraphicView mLineGraphicView2;
    private LineGraphicView mLineGraphicView3;
    private LineGraphicView mLineGraphicView4;
    private LineGraphicView mLineGraphicView5;
    private LineGraphicView mLineGraphicView6;
    private LineGraphicView mLineGraphicView7;
    private LineGraphicView mLineGraphicView8;

    private List<BSRecord> mBSRecords;

    private int count = 7;

    public static BloodSugarChartFragment7Days newInstance(String text){
        Bundle bundle = new Bundle();
        bundle.putString("text",text);
        BloodSugarChartFragment7Days bloodSugarChartFragment7Days = new BloodSugarChartFragment7Days();
        bloodSugarChartFragment7Days.setArguments(bundle);
        return  bloodSugarChartFragment7Days;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_blood_sugar_chart_7days, container, false);
        mLineGraphicView1 = view.findViewById(R.id.line_graphic1);
        mLineGraphicView2 = view.findViewById(R.id.line_graphic2);
        mLineGraphicView3 = view.findViewById(R.id.line_graphic3);
        mLineGraphicView4 = view.findViewById(R.id.line_graphic4);
        mLineGraphicView5 = view.findViewById(R.id.line_graphic5);
        mLineGraphicView6 = view.findViewById(R.id.line_graphic6);
        mLineGraphicView7 = view.findViewById(R.id.line_graphic7);
        mLineGraphicView8 = view.findViewById(R.id.line_graphic8);

        mLineGraphicView1.setHorizontalDrawText(true);
        mLineGraphicView2.setHorizontalDrawText(true);
        mLineGraphicView3.setHorizontalDrawText(true);
        mLineGraphicView4.setHorizontalDrawText(true);
        mLineGraphicView5.setHorizontalDrawText(true);
        mLineGraphicView6.setHorizontalDrawText(true);
        mLineGraphicView7.setHorizontalDrawText(true);
        mLineGraphicView8.setHorizontalDrawText(true);

        yList1 = new ArrayList<>();
        yList2 = new ArrayList<>();
        yList3 = new ArrayList<>();
        yList4 = new ArrayList<>();
        yList5 = new ArrayList<>();
        yList6 = new ArrayList<>();
        yList7 = new ArrayList<>();
        yList8 = new ArrayList<>();

        xRawDatas = new ArrayList<>();
        xRawDatas.add(DateUtil.date2Str(DateUtil.getPreDate(new Date(), 6), "MM/dd"));
        xRawDatas.add(DateUtil.date2Str(DateUtil.getPreDate(new Date(), 5), "MM/dd"));
        xRawDatas.add(DateUtil.date2Str(DateUtil.getPreDate(new Date(), 4), "MM/dd"));
        xRawDatas.add(DateUtil.date2Str(DateUtil.getPreDate(new Date(), 3), "MM/dd"));
        xRawDatas.add(DateUtil.date2Str(DateUtil.getPreDate(new Date(), 2), "MM/dd"));
        xRawDatas.add(DateUtil.date2Str(DateUtil.getPreDate(new Date(), 1), "MM/dd"));
        xRawDatas.add(DateUtil.date2Str(DateUtil.getPreDate(new Date(), 0), "MM/dd"));
        initView();

        return view;
    }

    public void initView(){
        if (yList1 == null)
            return;
        mBSRecords = BSRecordHolder.get(getContext()).getBSRecords(count);
        if (yList1.size() > 0){
            yList1.clear();
            yList2.clear();
            yList3.clear();
            yList4.clear();
            yList5.clear();
            yList6.clear();
            yList7.clear();
            yList8.clear();
        }
        for (int i = 0; i < count; i ++){
            BSRecord bsRecord = mBSRecords.get(i);
            yList1.add(bsRecord.getDawn());
            yList2.add(bsRecord.getBeforeBreakfast());
            yList3.add(bsRecord.getAfterBreakfast());
            yList4.add(bsRecord.getBeforeLunch());
            yList5.add(bsRecord.getAfterLunch());
            yList6.add(bsRecord.getBeforeDinner());
            yList7.add(bsRecord.getAfterDinner());
            yList8.add(bsRecord.getBeforeSleep());
        }

        mLineGraphicView1.setData(yList1, xRawDatas, 18, 2);
        mLineGraphicView2.setData(yList2, xRawDatas, 18, 2);
        mLineGraphicView3.setData(yList3, xRawDatas, 18, 2);
        mLineGraphicView4.setData(yList4, xRawDatas, 18, 2);
        mLineGraphicView5.setData(yList5, xRawDatas, 18, 2);
        mLineGraphicView6.setData(yList6, xRawDatas, 18, 2);
        mLineGraphicView7.setData(yList7, xRawDatas, 18, 2);
        mLineGraphicView8.setData(yList8, xRawDatas, 18, 2);
    }

    @Override
    public void onResume() {
        super.onResume();
        initView(); // 初始化界面（更新界面）
    }
}
